home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP05.ZIP / CHAP05 / PATRON / PAGEWIN.CPP < prev    next >
C/C++ Source or Header  |  1993-06-17  |  20KB  |  747 lines

  1. /*
  2.  * PAGEWIN.CPP
  3.  * Modifications for Chapter 5
  4.  *
  5.  * Window procedure for the Pages window and support functions.  This
  6.  * window manages its own scrollbars and viewport and provides
  7.  * printing capabilities as well.  The public CPages::Print lives here.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19.  
  20. #include "patron.h"
  21.  
  22.  
  23. extern HWND g_hDlgPrint;
  24. extern BOOL g_fCancelPrint;
  25.  
  26.  
  27. /*
  28.  * PagesWndProc
  29.  *
  30.  * Purpose:
  31.  *  Window procedure for the Pages window.
  32.  */
  33.  
  34. LRESULT __export FAR PASCAL PagesWndProc(HWND hWnd, UINT iMsg
  35.     , WPARAM wParam, LPARAM lParam)
  36.     {
  37.     LPCPages        ppg;
  38.     PAINTSTRUCT     ps;
  39.     HDC             hDC;
  40.     int             iPos, iTmp;
  41.     int             iMin, iMax;
  42.     UINT            idScroll;
  43.  
  44.  
  45.     ppg=(LPCPages)GetWindowLong(hWnd, PAGEWL_STRUCTURE);
  46.  
  47.     switch (iMsg)
  48.         {
  49.         case WM_CREATE:
  50.             ppg=(LPCPages)((LPCREATESTRUCT)lParam)->lpCreateParams;
  51.             SetWindowLong(hWnd, PAGEWL_STRUCTURE, (LONG)ppg);
  52.  
  53.             ppg->m_hWnd=hWnd;
  54.             break;
  55.  
  56.  
  57.         case WM_PAINT:
  58.             hDC=BeginPaint(hWnd, &ps);
  59.  
  60.             //Draw only if we have a page to show.
  61.             if (0!=ppg->m_cPages)
  62.                 ppg->Draw(hDC, FALSE, FALSE);
  63.  
  64.             EndPaint(hWnd, &ps);
  65.             break;
  66.  
  67.  
  68.         case WM_HSCROLL:
  69.         case WM_VSCROLL:
  70.             idScroll=(WM_HSCROLL==iMsg) ? SB_HORZ : SB_VERT;
  71.  
  72.             iPos=GetScrollPos(hWnd, idScroll);
  73.             iTmp=iPos;
  74.             GetScrollRange(hWnd, idScroll, &iMin, &iMax);
  75.  
  76.             switch (wParam)
  77.                 {
  78.                 case SB_LINEUP:     iPos -= 20;  break;
  79.                 case SB_PAGEUP:     iPos -=100;  break;
  80.                 case SB_LINEDOWN:   iPos += 20;  break;
  81.                 case SB_PAGEDOWN:   iPos +=100;  break;
  82.  
  83.                 case SB_THUMBPOSITION:
  84.                     iPos=ScrollThumbPosition(wParam, lParam);
  85.                     break;
  86.  
  87.                 //We don't want scrolling on this message.
  88.                 case SB_THUMBTRACK:
  89.                     return 0L;
  90.                 }
  91.  
  92.             iPos=max(iMin, min(iPos, iMax));
  93.  
  94.             if (iPos!=iTmp)
  95.                 {
  96.                 //Set the new position and scroll the window as necessary.
  97.                 SetScrollPos(hWnd, idScroll, iPos, TRUE);
  98.  
  99.                 if (SB_HORZ==idScroll)
  100.                     {
  101.                     ppg->m_xPos=iPos;
  102.                     ScrollWindow(hWnd, iTmp-iPos, 0, NULL, NULL);
  103.                     }
  104.                 else
  105.                     {
  106.                     ppg->m_yPos=iPos;
  107.                     ScrollWindow(hWnd, 0, iTmp-iPos, NULL, NULL);
  108.                     }
  109.                 }
  110.  
  111.             break;
  112.  
  113.         default:
  114.             return DefWindowProc(hWnd, iMsg, wParam, lParam);
  115.         }
  116.  
  117.     return 0L;
  118.     }
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. /*
  127.  * CPages::Draw
  128.  *
  129.  * Purpose:
  130.  *  Paints the current page in the pages window.
  131.  *
  132.  * Parameters:
  133.  *  hDC             HDC to draw on, could be a metafile or printer DC or
  134.  *                  any other type of DC.
  135.  *  fNoColor        BOOL indicating if we should use screen colors or
  136.  *                  printer colos (B&W).  Objects are printed as-is, however.
  137.  *                  This is TRUE for printer DCs or print preview.
  138.  *  fPrinter        BOOL indicating if this is a printer DC in which case
  139.  *                  we eliminate some of the fancy drawing, like shadows on
  140.  *                  the page and so forth.
  141.  *
  142.  * Return Value:
  143.  *  None
  144.  */
  145.  
  146. void CPages::Draw(HDC hDC, BOOL fNoColor, BOOL fPrinter)
  147.     {
  148.     RECT            rc, rcT;
  149.     UINT            uMM;
  150.     HPEN            hPen;
  151.     HBRUSH          hBrush;
  152.     HGDIOBJ         hObj1, hObj2;
  153.     COLORREF        cr;
  154.     char            szTemp[20];
  155.     UINT            cch;
  156.     DWORD           dwExt;
  157.  
  158.     //Make sure the DC is in LOMETRIC
  159.     uMM=SetMapMode(hDC, MM_LOMETRIC);
  160.  
  161.     if (!fPrinter)
  162.         {
  163.         /*
  164.          * We maintain a 6mm border around the page on the screen besides
  165.          * 12.7mm margins.  We also have to account for the scroll position
  166.          * with m_*Pos which are in pixels so we have to convert them.
  167.          */
  168.  
  169.         SetRect(&rcT, m_xPos, m_yPos, 0, 0);
  170.         RectConvertMappings(&rcT, hDC, FALSE);
  171.  
  172.         rc.left  = LOMETRIC_BORDER-rcT.left;
  173.         rc.top   =-LOMETRIC_BORDER-rcT.top;
  174.         }
  175.     else
  176.         {
  177.         /*
  178.          * We define the corner of the printed paper at a negative
  179.          * offset so rc.right and rc.bottom come out right below.
  180.          */
  181.         SetRect(&rc, -(int)m_xMarginLeft, m_yMarginTop, 0, 0);
  182.         }
  183.  
  184.     rc.right =rc.left+(UINT)m_cx+(UINT)(m_xMarginLeft+m_xMarginRight);
  185.     rc.bottom=rc.top -(UINT)m_cy-(UINT)(m_yMarginTop+m_yMarginBottom);
  186.  
  187.     //Draw a rectangle filled with the window color to show the page.
  188.     if (!fPrinter)
  189.         {
  190.         if (fNoColor)
  191.             {
  192.             //Black frame, white box for printed colors.
  193.             hPen  =CreatePen(PS_SOLID, 0, RGB(0,0,0));
  194.             hBrush=CreateSolidBrush(RGB(255, 255, 255));
  195.             }
  196.         else
  197.             {
  198.             //Normal colors on display
  199.             hPen  =CreatePen(PS_SOLID, 0, GetSysColor(COLOR_WINDOWFRAME));
  200.             hBrush=CreateSolidBrush(GetSysColor(COLOR_WINDOW));
  201.             }
  202.  
  203.         hObj1=SelectObject(hDC, hPen);
  204.         hObj2=SelectObject(hDC, hBrush);
  205.  
  206.         //Paper boundary
  207.         Rectangle(hDC, rc.left, rc.top, rc.right, rc.bottom+1);
  208.  
  209.         /*
  210.          * Draw a shadow on the *visual* bottom and right edges .5mm wide.
  211.          * If the button shadow color and workspace colors match, then
  212.          * use black.  We always use black when printing as well.
  213.          */
  214.         if (fNoColor)
  215.             cr=RGB(0,0,0);
  216.         else
  217.             {
  218.             cr=GetSysColor(COLOR_BTNSHADOW);
  219.  
  220.             if (GetSysColor(COLOR_APPWORKSPACE)==cr)
  221.                 cr=RGB(0,0,0);
  222.             }
  223.  
  224.         cr=SetBkColor(hDC, cr);
  225.         SetRect(&rcT, rc.left+5, rc.bottom, rc.right+5, rc.bottom-5);
  226.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  227.  
  228.         SetRect(&rcT, rc.right, rc.top-5, rc.right+5, rc.bottom-5);
  229.         ExtTextOut(hDC, 0, 0, ETO_OPAQUE, &rcT, NULL, 0, NULL);
  230.         SetBkColor(hDC, cr);
  231.  
  232.         SelectObject(hDC, hObj1);
  233.         SelectObject(hDC, hObj2);
  234.         DeleteObject(hBrush);
  235.         DeleteObject(hPen);
  236.         }
  237.  
  238.     //Write the page number in the lower left corner
  239.     if (!fNoColor)
  240.         {
  241.         SetTextColor(hDC, GetSysColor(COLOR_WINDOWTEXT));
  242.         SetBkColor(hDC, GetSysColor(COLOR_WINDOW));
  243.         }
  244.  
  245.     //Write the page number in our page font.
  246.     cch=wsprintf(szTemp, "Page %d", m_iPageCur+1);
  247.  
  248.     hObj1=SelectObject(hDC, m_hFont);
  249.     dwExt=GetTextExtent(hDC, szTemp, cch);
  250.  
  251.     TextOut(hDC, rc.left+m_xMarginLeft
  252.         , rc.bottom+m_yMarginBottom+HIWORD(dwExt), szTemp, cch);
  253.  
  254.     SelectObject(hDC, hObj1);
  255.  
  256.     //Rectangle to show border.
  257.     MoveTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  258.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.bottom+m_yMarginBottom);
  259.     LineTo(hDC, rc.right-m_xMarginRight, rc.bottom+m_yMarginBottom);
  260.     LineTo(hDC, rc.right-m_xMarginRight, rc.top-m_yMarginTop);
  261.     LineTo(hDC, rc.left+m_xMarginLeft,   rc.top-m_yMarginTop);
  262.  
  263.     SetMapMode(hDC, uMM);
  264.     return;
  265.     }
  266.  
  267.  
  268.  
  269.  
  270.  
  271. /*
  272.  * CPages::UpdateScrollRanges
  273.  *
  274.  * Purpose:
  275.  *  Reset scrollbar ranges (horizontal and vertical) depending on
  276.  *  the window size and the page size.  This function may remove the
  277.  *  scrollbars altogether.
  278.  *
  279.  * Parameters:
  280.  *  None, but set m_cx, m_cy and size m_hWnd before calling.
  281.  *
  282.  * Return Value:
  283.  *  None
  284.  */
  285.  
  286. void CPages::UpdateScrollRanges(void)
  287.     {
  288.     UINT        cxSB;   //Scrollbar width and height.
  289.     UINT